package com.admin.action;

import com.admin.bean.GoodSoftwareType;
import com.admin.service.GoodSoftwareTypeService;
import com.data.util.CheckDataUtils;
import com.data.util.ImageLookUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 精选软件类别管理
 *
 * @author:HingLo
 * @create 2017-09-30 16:44
 **/
@Controller
@RequestMapping("/goodSoftwareType/admin/")
public class GoodSoftwareTypeAction {
    @Autowired
    private GoodSoftwareTypeService goodSoftwareTypeService;


    @GetMapping("inputGoodSoftware")
    public String inputGoodSoftwareAction(HttpServletRequest request) {
        List<?> list = goodSoftwareTypeService.findAllGoodSoftwareTypeService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/banner/goodSoftwareType";
    }

    /**
     * 进入到添加GoodSoftware图界面
     *
     * @return
     */
    @GetMapping("addGoodSoftware")
    public String inputGoodSoftwareAddAdminAction() {
        return "/WEB-INF/admin/banner/addgoodSafewareType";
    }

    /**
     * 添加GoodSoftware图
     *
     * @param goodSoftwareType
     * @param request
     * @return
     */
    @PostMapping("submitGoodSoftwareAdd")
    public String submitGoodSoftwareAction(@RequestParam("file") CommonsMultipartFile file, GoodSoftwareType goodSoftwareType, String date, HttpServletRequest request) {
        boolean bool = false;
        if (goodSoftwareType != null) {
            goodSoftwareType.setIcon(file.getBytes());
            bool = goodSoftwareTypeService.submitGoodSoftwareAddAdminService(goodSoftwareType, date);
        }
        request.setAttribute("boolData", bool);
        return "/WEB-INF/admin/banner/addgoodSafewareType";

    }


    /**
     * 删除GoodSoftware图
     *
     * @param id
     * @param request
     * @return
     */
    @PostMapping("deleteGoodSoftware")
    @ResponseBody
    public String delGoodSoftwareAdminAction(String id, HttpServletRequest request) {
        boolean bool = goodSoftwareTypeService.delGoodSoftwareAdminService(id);
        return String.valueOf(bool);
    }

    @GetMapping("lookGoodSoftwareIcon")
    public void lookGoodSoftwareIconAction(String id, HttpServletResponse response) {
        if (CheckDataUtils.stringUtilsID(id)) {
            GoodSoftwareType goodSoftwareType = goodSoftwareTypeService.lookGoodSoftwareIconService(id);
            if (goodSoftwareType != null) {
                byte[] inputStream = goodSoftwareType.getIcon();
                ImageLookUtils.responseImage(inputStream, response);
            }
        }
    }


}
